package Thread;

class Demo{
    private static int count1;
    private static int count2;

    static   Object object1 = new Object();
    static   Object object2 = new Object();
   synchronized public static void countAdd() throws InterruptedException {
        count1++;
        Thread.sleep(10);
       System.out.println("count1 "+count1);
    }
   synchronized public static void countSub() throws InterruptedException {
            count2--;
       Thread.sleep(100);
       System.out.println("count2 "+count2);
    }
    public static int getCount() {
        return count1;
    }

    public static int getCount2() {
        return count2;
    }
}

public class ThreadDemo11 {

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                try {
                    Demo.countAdd();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        Thread t2 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                try {
                    Demo.countSub();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        //执行t1线程
        t1.start();

        //执行t2线程
        t2.start();

        //等待t1,t2线程执行完
        t1.join();
        t2.join();

        //打印此时的count值
        System.out.println(Demo.getCount());
        System.out.println(Demo.getCount2());
    }
}
